﻿//////////////////////////////////////////////////////////////////////////////
//
// Copyright © 1998-2024 Glodon Company Limited.
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the “Software”),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
//////////////////////////////////////////////////////////////////////////////
#include "AddinHandle.h"
#include "IAddinManager.h"
#include "GcmpDebugCommandNames.h"
#include "CommandIds.h"
#include "UiPlatformUtils.h"
#include "IRibbonContextualPage.h"
#include "EnableCompileWarning_The_LAST_IncludeInCpp.h" 

using namespace gcmp;
using namespace Sample;

namespace Sample
{
    std::vector<AddinButton> gDbgButtons{
        AddinButton{ID_CMD_SET_DEBUG_MODE, L"Debug Mode", L":/images/Test/48x48.png", {}},
        AddinButton{L"Regression Test", L"Regression Test", L":/images/Test/48x48.png", {
            AddinButton{ ID_CMD_REPLAY_JOURNAL, L"日志回放(脚本文件)", L":/images/Test/48x48.png", {}},
            AddinButton{ ID_CMD_REPLAY_JOURNAL_CONTENT, L"日志回放(js语句块)", L":/images/Test/48x48.png", {}},
            AddinButton{ ID_CMD_CONTINUOUS_REPLAY_JOURNAL, L"循环日志回放(脚本文件)", L":/images/Test/48x48.png", {}},
            AddinButton{ ID_CMD_UNIT_TEST, L"Unit Test", L":/images/Test/48x48.png", {}},
            AddinButton{ ID_CMD_API_TEST, L"GUnit Runner", L":/images/Test/48x48.png", {}},
        }},
        AddinButton{ L"日志验证器", L"日志验证器", L":/images/Test/48x48.png",{
            AddinButton{ ID_CMD_MODEL_VALIDATOR, L"添加验证器", L":/images/Test/48x48.png",{} },
            AddinButton{ ID_CMD_SNAPSHOT_VALIDATOR, L"截图比对", L":/images/Test/48x48.png",{} },
        }},
        AddinButton{ ID_CMD_RUNTIME_INFO, L"Runtime Information", L":/images/Test/48x48.png",{} },
        AddinButton{ L"Dump", L"Dump", L":/images/Test/48x48.png",{
            AddinButton{ ID_CMD_DUMP_ALL_ELEMENTS, L"All Elements", L":/images/Test/48x48.png",{} },
            AddinButton{ ID_CMD_FIX_SHORT_ELEMENTID, L"Fix Short Id", L":/images/Test/48x48.png",{} },
            AddinButton{ ID_CMD_DUMP_ANCESTORS_OF_SELECTED_AS_PNG, L"Dump Ancestors of Selection", L":/images/Test/48x48.png",{} },
            AddinButton{ ID_CMD_DUMP_CHILDREN_OF_SELECTED, L"Dump Children of Selection", L":/images/Test/48x48.png",{} },
            AddinButton{ ID_CMD_DUMP_DECENDANTS_OF_SELECTED_AS_DOT, L"Dump Decendants of Selection", L":/images/Test/48x48.png",{} },
            AddinButton{ ID_CMD_DUMP_OBJECT_RELATIONSHIPS_OF_SELECTED_AS_DOT, L"Dump Relationships of Selection", L":/images/Test/48x48.png",{} },
            AddinButton{ ID_CMD_DUMP_CURRENT_CLASS_SCHEMAS_AS_XML, L"导出ClassSchemas为XML格式", L":/images/Test/48x48.png",{} }
        }},
        AddinButton{ L"GRep", L"GRep", L":/images/Test/48x48.png",{
            AddinButton{ ID_CMD_GREP_TO_JSON, L"Dump GRep As Json", L":/images/Test/48x48.png",{} },
            AddinButton{ ID_CMD_CREATE_GREP_FROM_JSON, L"Create GRep From Json", L":/images/Test/48x48.png",{} },
            AddinButton{ ID_CMD_GREP_TO_JSON_AND_FROM_JSON, L"Dump GRep As Json And From JSON", L":/images/Test/48x48.png",{} },
        }},
        AddinButton{ ID_CMD_COLLECT_NEED_UPGRADE_CLASSID, L"收集文档中需要升级的类信息", L":/images/Test/48x48.png",{} },
        AddinButton{ L"Ndb对象创建情况", L"Ndb对象创建情况", L":/images/Test/48x48.png",{
            AddinButton{ ID_CMD_BEGIN_NDB_ELEMENT_CREATION_STATS, L"开始统计", L":/images/Test/48x48.png",{} },
            AddinButton{ ID_CMD_END_NDB_ELEMENT_CREATION_STATS, L"结束统计（在Dump文件里）", L":/images/Test/48x48.png",{} },
        }},
        AddinButton{ ID_CMD_SELECT_BY_ID, L"Select By-Id", L":/images/Test/48x48.png",{} },
        AddinButton{ ID_CMD_PICK_4_DEVELOPER, L"Pick", L":/images/Test/48x48.png",{} },
        AddinButton{ ID_CMD_RELOAD_FAMILIES, L"Reload Existing Families(From temp file other than user's file)", L":/images/Test/48x48.png",{} },
        AddinButton{ ID_CMD_SAVE_ELEMENTS_BY_CATEGORY, L"Save Elements By Category", L":/images/Test/48x48.png",{} },

        AddinButton{ L"Regen", L"Regen", L":/images/Test/48x48.png",{
            AddinButton{ ID_CMD_REGENERATE_ALL, L"Force Regen Project Doc", L":/images/Test/48x48.png",{} },
            AddinButton{ ID_CMD_REGENERATE_Project_And_Family_DOCMENTS, L"Force Regen Project And Family Docs", L":/images/Test/48x48.png",{} },
            AddinButton{ ID_CMD_REGENERATE_SELECTED, L"Force Regen Selected", L":/images/Test/48x48.png",{} },
        }},

        AddinButton{ L"RelationShip", L"RelationShip", L":/images/Test/48x48.png",{
            AddinButton{ ID_CMD_ELIMINATE_CUT_RELATIONSHIP, L"Eliminate Cut Relationship", L":/images/Test/48x48.png",{} },
            AddinButton{ ID_CMD_ELIMINATE_JOIN_RELATIONSHIP, L"Eliminate Join Relationship", L":/images/Test/48x48.png",{} },
        }},

        AddinButton{ L"链接", L"链接", L":/images/Test/48x48.png",{
            AddinButton{ ID_CMD_DELETE_UNLOADED_LINK_DOCUMENT_TYPE, L"删除未连接状态的链接文档类型对象", L":/images/Test/48x48.png",{} },
            AddinButton{ ID_CMD_DELETE_UNRECORDED_PROXIES, L"删除未被记录的代理对象", L":/images/Test/48x48.png",{} },
        }}
    };
}

bool AddinHandle::Setup()
{
    IAddinManager* pAddinManager = IAddinManager::Get();
    if (nullptr == pAddinManager)
    {
        return false;
    }
    std::vector<std::wstring> addinIds = pAddinManager->GetAllAddinIds();
    if (addinIds.empty())
    {
        return false;
    }
    for (auto& id : addinIds)
    {
        if (id.compare(L"GcmpDebug") == 0)
        {
            SetupDebugTools();
        }

    }
    return true;
}

void Sample::AddinHandle::SetupDebugTools()
{
    std::wstring pageName = L"开发者工具";
    std::wstring groupName = L"开发者工具";
    std::wstring pageId = L"page_" + pageName;
    std::wstring groupId = L"group_" + pageName;
    UiPlatformUtils::AddPage(pageId, pageName);
    IRibbonGroup* pGroup = UiPlatformUtils::GetGroup(pageId, groupId, groupName);
    if (pGroup)
    {
        UiPlatformUtils::LoadButtons(pGroup, gDbgButtons);
    }
}

